﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using TbShop.Web.Infrastructure;
using HeinQi.Framework.Common;

namespace TbShop.Web.Infrastructure
{
    public class StartupHandleErrorLogAttribute : HandleErrorLogAttribute
    {
        /// <summary>
        /// 系统错误处理
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnException(System.Web.Mvc.ExceptionContext filterContext)
        {
            base.OnException(filterContext);
            filterContext.ExceptionHandled = true;
            LogHelper.Error(filterContext.Exception.Message);
            if (filterContext.RouteData.DataTokens["area"] != null && filterContext.RouteData.DataTokens["area"].ToString().ToUpper() == "ADMIN")
            {
                //filterContext.Result = new RedirectResult("~/Admin/Error/Error_500");
            }
            else
            {
                if (HeinQi.Framework.Common.SystemParameterHelper.GetSystemParameter<bool>("ErrorPage"))
                {
                    if (filterContext.Exception is CustomerException.NotFoundException)
                    {
                        filterContext.Result = new RedirectResult("~/Error/Error_404");
                    }
                    else if (filterContext.Exception is CustomerException.PostException)
                    {
                        HttpContext.Current.Session["alert_message"] = filterContext.Exception.Message;
                        HttpContext.Current.Session["alert_message_type"] = "warning";
                        filterContext.Result = new RedirectResult(HttpContext.Current.Request.ServerVariables["HTTP_REFERER"]);
                    }
                    else
                    {
                        filterContext.Result = new RedirectResult("~/Error/Error_500");
                    }
                }
            }
        }
    }
}